/*
 * This file is part of the Boring Engine.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.googlecode.boringengine;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;

public class Config {

    private static HashMap<String, String> config = new HashMap<String, String>();

    public static void init() {
        File f = new File(GameSettings.configDir + "config");
        if (!f.exists()) {
            Log.info("Config file doesn't exist, using defaults");
            try {
                f.createNewFile();
            } catch (IOException ex) {
                Log.error("Couldn't create config file, saving will not work");
            }
            return;
        }
        Scanner in;
        try {
            in = new Scanner(f);
        } catch (FileNotFoundException ex) {
            Log.error("Couldn't open config file, using defaults: %s", ex.getMessage());
            return;
        }
        while (in.hasNextLine()) {
            String line = in.nextLine();
            String[] tokens = line.split("=", 2);
            config.put(tokens[0].trim(), tokens[1].trim());
        }
    }

    public static void deinit() {
        PrintStream out;
        try {
            out = new PrintStream(GameSettings.configDir + "config");
        } catch (FileNotFoundException ex) {
            Log.error("Couldn't save config: ", ex.getMessage());
            return;
        }
        for (String key : config.keySet())
            out.printf("%s = %s\n", key, config.get(key));
        out.close();
    }

    public static void crash_deinit() {
        PrintStream out;
        try {
            out = new PrintStream(GameSettings.configDir + "crash_config");
        } catch (FileNotFoundException ex) {
            Log.error("Couldn't save config: ", ex.getMessage());
            return;
        }
        for (String key : config.keySet())
            out.printf("%s = %s\n", key, config.get(key));
        out.close();

    }

    public static boolean getBoolean(String key, boolean def) {
        if (!config.containsKey(key)) {
            set(key, def);
            return def;
        }
        return Boolean.parseBoolean(config.get(key));
    }

    public static String getString(String key, String def) {
        if (!config.containsKey(key)) {
            set(key, def);
            return def;
        }
        return config.get(key);
    }

    public static void set(String key, boolean val) {
        config.put(key, Boolean.toString(val));
    }

    public static void set(String key, String val) {
        if (val == null)
            config.remove(key);
        else
            config.put(key, val);
    }

}
